package com.gary;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author pengrui
 * @date 2020/12/16 - 16:16
 */
public class JfreePaint {

    public static void main(String[] args) {

        //ByteArrayOutputStream bos = null;
        //Integer len = 0;
        //try {
        //    ChartUtilities.writeChartAsPNG(byteArrayOut, birthday, 400, 200);
        //    String fileSavePath = "exTest.png";
        //    BufferedImage bufferImg = ImageIO.read(new File(fileSavePath));
        //    ImageIO.write(bufferImg, "png", byteArrayOut);
        //} catch (IOException e) {
        //}
        //ChartFrame frame = new ChartFrame("birthday", birthday);
        //frame.pack();
        //frame.setVisible(true);

    }

    public String pipePhoto(HashMap<String, Object> date, String photoName)  throws IOException {
        File file = new File("E:/workDoc/photo/"+photoName+".png");
        //创建数据源
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        //设置数据
        pieDataset.setValue("2,000-2,006", 20);
        pieDataset.setValue("1,990-1,999", 50);
        pieDataset.setValue("1,980-1,989", 15);
        pieDataset.setValue(">1,949", 15);
        //创建饼图
        JFreeChart birthday = ChartFactory.createPieChart3D("出生年份", pieDataset);
        birthday.setBackgroundPaint(new Color(255, 255, 255));
        PiePlot plot = (PiePlot) birthday.getPlot();
        plot.setSectionOutlinePaint("2,000-2,006", new Color(91, 155, 213));
        plot.setSectionOutlinePaint("1,990-1,999", new Color(255, 192, 0));
        plot.setSectionOutlinePaint("1,980-1,989", new Color(237, 125, 49));
        plot.setSectionOutlinePaint(">1,949", new Color(165, 165, 165));
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}, [{1}--{2}]"));

        ChartUtilities.saveChartAsPNG(file, birthday, 800, 500);
        return null;
    }
}
